package com.hellpro.lolbox.mine.view;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.LolBaseActivity;
import com.hellpro.lolbox.mine.presenter.UserInfoPresenter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.Bmob;

public class UserInfoActivity extends LolBaseActivity implements IUserInfoActivity {


    @BindView(R.id.main_userName_iv_return)
    ImageView mainUserNameIvReturn;
    @BindView(R.id.mine_userInfo_icon)
    ImageView mineUserInfoIcon;
    @BindView(R.id.mine_userInfo_nickName)
    EditText mineUserInfoNickName;
    @BindView(R.id.mine_userInfo_birthday)
    EditText mineUserInfoBirthday;
    @BindView(R.id.mine_userInfo_btn_register)
    Button mineUserInfoBtnRegister;
    @BindView(R.id.mine_userInfo_man)
    RadioButton mineUserInfoMan;
    @BindView(R.id.mine_userInfo_woman)
    RadioButton mineUserInfoWoman;
    @BindView(R.id.mine_userInfo_sex)
    RadioGroup mineUserInfoSex;
    private UserInfoPresenter presenter;
    private LinearLayout view;
    private int CAMERA = 1;
    private int PICTURE = 2;
    private String phoneNo;
    private Intent intent;

    @Override
    public int getLayoutId() {
        return R.layout.activity_user_info;
    }

    @Override
    public void initView() {

        Bmob.initialize(getApplicationContext(), "b9e80a06ccaa5632b74a3e3f12aedc9b");
        intent = getIntent();

        presenter = new UserInfoPresenter(this);

        view = (LinearLayout) LayoutInflater.from(this)
                .inflate(R.layout.popup_window_item, null);
        Button btnCamera = (Button) view.findViewById(R.id.popup_btn_camera);
        btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(camera, CAMERA);
            }
        });
        Button btnAlbum = (Button) view.findViewById(R.id.popup_btn_album);
        btnAlbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent picture = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(picture, PICTURE);
            }
        });

    }

    @OnClick({R.id.mine_userInfo_icon, R.id.mine_userInfo_btn_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mine_userInfo_icon:
                //Todo:头像设置
                presenter.setUserNameIcon();
                break;
            //完成
            case R.id.mine_userInfo_btn_register:
                presenter.setUserInfoToBmob();
//                UIManger.startToActivity(this, MainActivity.class);
                finish();
                break;
        }
    }

    @Override
    public EditText getEtUserNickName() {
        return mineUserInfoNickName;
    }

    @Override
    public EditText getEtUserBirthday() {
        return mineUserInfoBirthday;
    }

    @Override
    public Button getBtnRegister() {
        return mineUserInfoBtnRegister;
    }

    @Override
    public String getPhoneNo() {

        return intent.getStringExtra("phoneNo");

    }

    @Override
    public String getPassword() {
        return intent.getStringExtra("password");
    }

    @Override
    public Context getUserInfoContext() {
        return UserInfoActivity.this;
    }

    @Override
    public RadioGroup getSex() {
        return mineUserInfoSex;
    }

    @Override
    public ImageView getUserInfoIvShow() {
        return mineUserInfoIcon;
    }

    @Override
    public LinearLayout getPopuWindow() {

        return view;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == CAMERA && resultCode == Activity.RESULT_OK && null != data){
            String sdState= Environment.getExternalStorageState();
            if(!sdState.equals(Environment.MEDIA_MOUNTED)){
                return;
            }
            new DateFormat();
            String name= DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA))+".jpg";
            Bundle bundle = data.getExtras();
            //获取相机返回的数据，并转换为图片格式
            Bitmap bitmap = (Bitmap)bundle.get("data");
            FileOutputStream fout = null;
            File file = new File("/sdcard/pintu/");
            file.mkdirs();
            String filename=file.getPath()+name;
            try {
                fout = new FileOutputStream(filename);
                assert bitmap != null;
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally{
                try {
                    assert fout != null;
                    fout.flush();
                    fout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //显示图片
            mineUserInfoIcon.setImageURI(Uri.parse(filename));
        }

        //从相册中选择图片
        if(requestCode == PICTURE && resultCode == Activity.RESULT_OK && null != data){

            Bitmap bm = null;

            //外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口
            ContentResolver resolver = getContentResolver();

            //此处的用于判断接收的Activity是不是你想要的那个
                try {
                    Uri originalUri = data.getData();        //获得图片的uri

                    bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);        //显得到bitmap图片

                    String[] proj = {MediaStore.Images.Media.DATA};

                    //好像是Android多媒体数据库的封装接口，具体的看Android文档
                    Cursor cursor = managedQuery(originalUri, proj, null, null, null);
                    //按我个人理解 这个是获得用户选择的图片的索引值
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    //将光标移至开头 ，这个很重要，不小心很容易引起越界
                    cursor.moveToFirst();
                    //最后根据索引值获取图片路径
                    String path1 = cursor.getString(column_index);
                    mineUserInfoIcon.setImageURI(Uri.parse(path1));
                }catch (IOException e) {
                    Log.e("TAG",e.toString());
                }


        }
    }

}
